<?php 
function GetAddresses()
{
        $conn = getConnection();
        $result = $conn->query('SELECT * FROM Addresses A');
        return $result;
}

function GetAddress($id)
{
        $conn = getConnection();
        $result = $conn->query("SELECT * FROM Addresses A WHERE ID=$id");
        $rs = $result->fetch_assoc();
        $conn->close();
        return $rs;
}

function SaveAddress($rs)
{ 
		$conn = getConnection();
		$rs = RsEscape($conn, $rs);
        $result = $conn->query("UPDATE Addresses A SET Address_Line1='$rs[Address_Line1]', Address_Line2='$rs[Address_Line2]', City='$rs[City]' , State='$rs[State]', Zipcode='$rs[Zipcode]', Country='$rs[Country]' WHERE ID=$rs[ID]");     
        $error = $conn->error;
        $conn->close();
        return $error ? array('Server Error'=> $error) : Null;
        
}

function ValidateAddress($rs)
{ 
	$errors = Null;
	if(empty($rs['Address_Line1']))
	{
		$errors['Address_Line1'] = 'Address Line 1 is required';
	}
	if(empty($rs['City']))
	{
		$errors['City'] = 'City is required';
	}
	if(empty($rs['State']))
	{
		$errors['State'] = 'State is required';
	}
	if(empty($rs['Zipcode']))
	{
		$errors['Zipcode'] = 'Zipcode is required';
	}
	
	return $errors;
	
}

function DeleteAddress($id)
{
	
		$conn = getConnection();
        $result = $conn->query("DELETE FROM Addresses WHERE ID=$id");     
        $error = $conn->error;
        $conn->close();
		return $error ? array('Server Error'=> $error) : Null;
	
}

function AddAddress($rs)
{
		$conn = getConnection();
		$rs = RsEscape($conn, $rs);
		$sql = "INSERT INTO Addresses" 
			   ."(Address_Line1, Address_Line2, City, State, Zipcode, Country, Created_at)"
			   ."VALUES ('$rs[Address_Line1]', '$rs[Address_Line2]', '$rs[City]', '$rs[State]', '$rs[Zipcode]', '$rs[Country]' , Now())";
        $result = $conn->query($sql);     
        $error = $conn->error;
        $conn->close();
		return $error ? array('Server Error'=> $error) : Null;
	
}

function NewAddress()
{
	return array('Address_Line1'=>'', 'Address_Line2'=>'', 'City'=>'', 'State'=>'', 'Zipcode'=>'', 'Country'=>' ', 'Created_at'=>Null, 'Updated_at'=>Null);
}

?>